package cn.kgc.service;

import cn.kgc.client.RestDmLinkUserClient;
import cn.kgc.client.RestDmOrderClient;
import cn.kgc.client.RestDmOrderLinkUserClient;
import cn.kgc.client.RestDmSchedulerSeatClient;
import cn.kgc.pojo.DmOrderLinkUser;
import cn.kgc.pojo.DmSchedulerSeat;
import cn.kgc.utils.Constants;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class OrderServiceConsumer {

    @Autowired
    RestDmSchedulerSeatClient restDmSchedulerSeatClient;

    @Autowired
    RestDmOrderClient restDmOrderClient;

    @Autowired
    RestDmOrderLinkUserClient restDmOrderLinkUserClient;

    //监听重置联系人队列
    @RabbitListener(queues= Constants.RabbitQueueName.TO_RESET_LINKUSER_QUQUE)
    public void resetLinkedUser(Integer orderId, Message message, Channel channel) throws IOException {
        try {
            Map map =new HashMap();
            map.put("orderId",orderId);
            List<DmOrderLinkUser> dmLinkUserListByMap = restDmOrderLinkUserClient.getDmOrderLinkUserListByMap(map);
            for (DmOrderLinkUser dmOrderLinkUser : dmLinkUserListByMap) {
                restDmOrderLinkUserClient.qdtxDelDmOrderLinkUser(dmOrderLinkUser.getId());
            }
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            e.printStackTrace();
            channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);
        }
    }

    //监听重置座位队列
    @RabbitListener(queues= Constants.RabbitQueueName.TO_RESET_SEAT_QUQUE)
    public void resetSeatMsg(Map map, Message message, Channel channel) throws IOException {
        java.util.List<DmSchedulerSeat>  seats = (List<DmSchedulerSeat>) map.get("seats");
        for (DmSchedulerSeat bookSeat : seats) {
            bookSeat.setStatus(Constants.SchedulerSeatStatus.SchedulerSeat_FREE);
            bookSeat.setOrderNo("");
            try {
                restDmSchedulerSeatClient.qdtxModifyDmSchedulerSeat(bookSeat);
                channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            } catch (Exception e) {
                e.printStackTrace();
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);
            }
        }
    }

    //监听删除订单队列
    @RabbitListener(queues= Constants.RabbitQueueName.TO_DEL_ORDER_QUQUE)
    public void DelOrder(Integer orderId, Message message, Channel channel) throws IOException {

        try {
            restDmOrderClient.qdtxDelDmOrder(new Long(orderId));
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            e.printStackTrace();
            channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);
        }
    }
}
